import {topCategory} from '@/api/constains'
import {findAllCategory} from '@/api/category'


export default {
    namespaced:true,
    state(){
        return {
            // 加载分类默认数据， 防止项目一上来就白屏
            list:topCategory.map((item,i)=>({name:item,id:i}))
        }
    },
    mutations:{
        SET_CATEGORY(state,payload){
            state.list = payload
        },
        // 修改二级分类显示与隐藏
        SHOW(state,id){
            const category = state.list.find(item=>item.id===id)
            // opem为一级类目的显示|隐藏 二级类目
            category.open = true
        },
        HIDE(state,id){
            const category = state.list.find(item=>item.id===id)
            category.open = false
        }
        
    },
    actions:{
       async getCateList({commit}){
        const res = await findAllCategory()
        // 给每个一级分类添加 显示、隐藏 
        res.result.forEach(v => {
            v.open = false
        });
        commit('SET_CATEGORY', res.result)
        }
    }
}